package com.zyb.warehouse.service.impl;

import com.zyb.warehouse.common.Result;
import com.zyb.warehouse.dto.Page;
import com.zyb.warehouse.entity.CurrentUser;
import com.zyb.warehouse.entity.Product;
import com.zyb.warehouse.mapper.ProductMapper;
import com.zyb.warehouse.service.ProductService;
import com.zyb.warehouse.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Description:商品表业务层接口实现类
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 分页查询商品
     *
     * @param page
     * @param product
     * @return
     */
    public Page pageProduct(Page page, Product product) {
        //查询总记录数
        Integer count = productMapper.selectProductTotal(product);
        //开始分页
        List<Product> productList = productMapper.pageProduct(page, product);

        //设置属性
        page.setTotalNum(count);
        page.setResultList(productList);
        return page;
    }

    /**
     * 添加商品
     * 将配置文件的file.access-path属性值注入给service的accessPath属性,
     * 其为上传的图片保存到数据库中的访问地址的目录路径/img/upload/;
     *
     * @param product
     * @param token
     * @return
     */
    @Value("${file.access-path}")
    private String accessPath;

    @Transactional
    public Result addProduct(Product product, String token) {
        //查询商品编号是否重复
        Product p = productMapper.selectProductByNum(product.getProductNum());
        if (p != null) {
            return Result.err(Result.CODE_ERR_BUSINESS, "该商品编号已存在");
        }
        //获取前端传递过来的token
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //设置属性
        product.setImgs(accessPath + product.getImgs());
        product.setCreateBy(currentUser.getUserId());
        //进行添加
        Integer i = productMapper.insertProduct(product);
        if (i > 0) {
            return Result.ok("添加商品成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "添加商品失败");
    }

    /**
     * 修改商品的上下架状态
     *
     * @param product
     * @param token
     * @return
     */
    @Transactional
    public Result changeProductState(Product product, String token) {
        //获取前端传递过来的token
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //设置属性
        product.setUpdateBy(currentUser.getUserId());
        //修改状态
        Integer i = productMapper.updateProductSate(product);
        if (i > 0) {
            return Result.ok("商品状态修改成功");
        }
        return Result.ok("商品状态修改失败");
    }

    /**
     * 删除商品
     *
     * @param productIds
     * @return
     */
    @Transactional
    public Result removeProduct(List<Integer> productIds) {
        //遍历商品Id
        for (Integer productId : productIds) {
            Product product = productMapper.selectProductById(productId);
            //上架中的商品不能删除
            if (product.getUpDownState().equals("1")) {
                return Result.err(Result.CODE_ERR_BUSINESS, "上架中的商品不能删除");
            }
        }
        //进行删除
        Integer i = productMapper.deleteProduct(productIds);
        if (i > 0) {
            return Result.ok("商品删除成功");
        }
        return Result.ok("商品删除失败");
    }

    /**
     * 修改商品
     *
     * @param product
     * @param token
     * @return
     */
    @Transactional
    public Result modifyProduct(Product product, String token) {
        //根据商品编号查询编号是否重复
        Product p = productMapper.selectProductByNum(product.getProductNum());
        if (p != null && !p.getProductId().equals(product.getProductId())) {
            return Result.err(Result.CODE_ERR_BUSINESS, "商品编号不能重复");
        }
        //判断商品图片是否修改了
        if (!product.getImgs().contains(accessPath)) {
            product.setImgs(accessPath + product.getImgs());
        }
        //获取前端传递过来的token
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        product.setUpdateBy(currentUser.getUserId());
        product.setUpdateTime(new Date());
        //进行修改
        Integer i = productMapper.updateProduct(product);
        if (i > 0) {
            return Result.ok("商品修改成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "商品修改失败");
    }
}
